package cn.tedu.ivos.application.service.impl;

import cn.tedu.ivos.application.mapper.ApplicationMapper;
import cn.tedu.ivos.application.pojo.dto.ApplicationQuery;
import cn.tedu.ivos.application.pojo.dto.ApplicationSaveParam;
import cn.tedu.ivos.application.pojo.entity.Application;
import cn.tedu.ivos.application.pojo.vo.ApplicationVO;
import cn.tedu.ivos.application.service.ApplicationService;
import cn.tedu.ivos.audit.mapper.AuditMapper;
import cn.tedu.ivos.audit.pojo.vo.AuditVO;
import cn.tedu.ivos.audit.service.AuditService;
import cn.tedu.ivos.base.enums.ApplicationStatusEnum;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringJoiner;

@Transactional
@Slf4j
@Service
public class ApplicationServiceImpl implements ApplicationService {
    @Autowired
    ApplicationMapper applicationMapper;
    @Autowired
    AuditService auditService;
    @Autowired
    AuditMapper auditMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    VehicleMapper vehicleMapper;

    @Override
    public void save(ApplicationSaveParam applicationSaveParam) {
        Application application = new Application();
        //将前端传过来的数据赋值给application对象
        BeanUtils.copyProperties(applicationSaveParam,application);
        //新增的申请单,初始状态为"已发起"
        application.setStatus(ApplicationStatusEnum.PENDING.getCode());
        application.setCreateTime(new Date());
        //由于业务原因,没有修改,可以实现撤销,然后重新发申请
        /*遇到的问题:新增申请对应的审批数据,但审批数据里没有申请单的id,怎么办?*/
        //因为我们要给即将生成的审批单设置申请单id,但申请单的id无法通过任何条件select获取
        //所以需要给此insert方法对应的SQL上进行配置:useGeneratedKeys="true" keyProperty="id"
        //这两句代码的作用是让jdbc为当前的application对象自动生成自增主键
        //并将自增主键的值赋值给当前application对象的id字段
        applicationMapper.insert(application);

        //还需要调用审批业务层,为当前申请单同步新增对应的审批数据
        auditService.insertAudit(application);
    }

    @Override
    public List<ApplicationVO> selectApplication(ApplicationQuery applicationQuery) {
        log.debug("查询申请单列表数据,applicationQuery={}",applicationQuery);
        //根据查询条件,查询符合的申请单数据
        List<ApplicationVO> list = applicationMapper.selectApplication(applicationQuery);
        //遍历申请单集合,依次查出每个申请单对应的审批人数据
        for (int i = 0; i<list.size(); i++) {
            ApplicationVO applicationVO =  list.get(i);
            //申请单列表中需要审批人的相关信息,我们可以自定义方法给applicationVO设置相关的审批人数据
            assignAuditUserList(applicationVO);
        }
        return list;
    }

    @Override
    public void cancel(Long id) {
        log.debug("撤销申请单业务:id={}",id);
        Application application = new Application();
        application.setId(id);
        //撤销需要将申请单状态改为"撤销"
        application.setStatus(ApplicationStatusEnum.CANCEL.getCode());
        application.setUpdateTime(new Date());
        applicationMapper.update(application);

        //还需要删除此申请单对应的所有审批单数据
        auditMapper.deleteByApplicationId(id);
    }

    @Override
    public void distribute(Long applicationId, Long vehicleId) {
        log.debug("分配车辆业务:applicationId={},vehicleId={}",applicationId,vehicleId);
        Application application = new Application();
        application.setId(applicationId);//设置要更新哪个申请单
        application.setVehicleId(vehicleId);//设置给当前申请单分配的车辆
        application.setStatus(ApplicationStatusEnum.ALLOCATION.getCode());//设置申请单状态为"已分配用车"
        application.setUpdateTime(new Date());//设置更新时间
        applicationMapper.update(application);//执行申请表更新操作
        //车辆也需要改为占用状态
        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);//设置要更新哪辆车
        vehicle.setStatus("2");//设置车辆状态为"占用"
        vehicleMapper.update(vehicle);//执行车辆表更新操作
    }

    @Override
    public void back(Long applicationId, Long vehicleId) {
        log.debug("归还车辆业务:applicationId={},vehicleId={}",applicationId,vehicleId);
        Application application = new Application();
        application.setId(applicationId);//设置要更新哪个申请单
        application.setVehicleId(vehicleId);//设置给当前申请单分配的车辆
        application.setStatus(ApplicationStatusEnum.END.getCode());//设置申请单状态为"工单结束"
        application.setUpdateTime(new Date());//设置更新时间
        applicationMapper.update(application);//执行申请表更新操作
        //车辆也需要改为占用状态
        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);//设置要更新哪辆车
        vehicle.setStatus("1");//设置车辆状态为"空闲"
        vehicleMapper.update(vehicle);//执行车辆表更新操作
    }

    /*
     * 本方法用于给当前applicationVO对象的auditUserIdList与auditUsernameList属性赋值
     * 也就是给当前申请单补充对应的审批人数据
     * @param applicationVO
     */
    private void assignAuditUserList(ApplicationVO applicationVO) {
        //1.准备空集合,分别用来保存当前申请单对应的审批人姓名集合与审批人id集合
        List<String> auditUsernameList = new ArrayList<>();
        List<Long> auditUserIdList = new ArrayList<>();
        //2.根据传入的申请单,查出对应的审批单数据,注意:一个申请单可能对应多个审批单!
        List<AuditVO> auditVOList= auditMapper.selectAuditByApplicationId(applicationVO.getId());
        //3.依次遍历出每一个审批单对象
        for (int i = 0; i<auditVOList.size(); i++){
            //4.取出当前遍历到的审批单对象
            AuditVO auditVO = auditVOList.get(i);
            //5.取出当前审批单对应的审批人id并装入上方的空id集合中
            Long id = auditVO.getAuditUserId();
            auditUserIdList.add(id);
            //6.根据审批单的审批人id,到用户数据层查出对应审批人的用户数据
            UserVO userVO = userMapper.selectById(id);
            //7.再将查出来的审批人姓名装入上方的空username集合中
            auditUsernameList.add(userVO.getUsername());
        }
        //8.循环结束后将准备好的审批人id集合赋值给applicationVO
        applicationVO.setAuditUserIdList(auditUserIdList);
        //9.准备一个拼接工具,帮我们把所有的审批人姓名连成一个字符串
        //stringJoiner.add("moly");stringJoiner.add("tom");的结果是 moly,tom
        StringJoiner stringJoiner = new StringJoiner(",");
        for (String username : auditUsernameList){
            stringJoiner.add(username);
        }
        //将拼接好的审批人姓名"moly,tom"赋值给applicationVO的auditUsernameList属性
        applicationVO.setAuditUsernameList(stringJoiner.toString());
    }


}





